package com.qianyitian.hope2.analyzer.gaokao;

import com.alibaba.fastjson.JSON;
import com.google.common.io.Files;
import com.qianyitian.hope2.analyzer.funds.model.PositionStatus;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Processor {
    public static String ROOT_PATH = "C:\\Users\\bing.a.qian\\Desktop\\高考\\all";
    public static String CSV_ROOT_PATH = "C:\\Users\\bing.a.qian\\Desktop\\高考";

    public static void main(String[] args) throws IOException {

        File root = new File(ROOT_PATH);
        File[] files = root.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.contains("本科-本科批-2019")) {
                    return true;
                } else {
                    return false;
                }
            }
        });


        List<MatriculateList> all = new ArrayList<>();
        for (File file : files) {
            String content = Files.asCharSource(file, Charset.forName("UTF-8")).readFirstLine();
            RootBean rootBean = JSON.parseObject(content, RootBean.class);
            all.addAll(rootBean.getData().getMatriculateList());
        }

        all = all.stream().sorted((m1, m2) -> m1.getRank() - m2.getRank()).collect(Collectors.toList());

        all.stream().forEach(item -> {

        });
        System.out.println(all.size());

        String json = genChartData(all);
        System.out.println(json);
        genCSV(all);
    }

    private static String genChartData(List<MatriculateList> all) {
        Map<String, List<Section>> map = new HashMap<>();

        List<Section> type1 = new ArrayList<>();
        List<Section> type2 = new ArrayList<>();

        all.stream().forEach(item -> {

            Section section = new Section();
            section.setX(item.getLowestScore());
            section.setY(item.getRank());
//            section.setLabel(item.getCollegeName());
//            section.setSubLabel(item.getMajorName());

            if (item.getSubjectName().equals("理工")) {
                type1.add(section);
            } else {
                type2.add(section);
            }
        });

        assert type1.size() + type2.size() == all.size();

        map.put("type1", type1);
        map.put("type2", type2);

        String content = JSON.toJSONString(map);

        File to = new File(CSV_ROOT_PATH, "report-2021.json");
        try {
            Files.asCharSink(to, Charset.forName("UTF-8")).write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }


        return content;
    }

    private static void genCSV(List<MatriculateList> all) {
        String splitter = ",";
        StringBuilder sb = new StringBuilder();
        all.stream().forEach(item -> {
            String newLine = System.getProperty("line.separator");
            sb.append(item.getRank()).append(splitter).append(item.getCollegeName()).append(splitter).append(item.getMajorName()).append(splitter)
                    .append(item.getLowestScore()).append(splitter).append(item.getAverageScore()).append(splitter)
                    .append(item.getBatchName()).append(splitter).append(item.getSubjectName()).append(newLine);

        });


        File to = new File(CSV_ROOT_PATH, "report.csv");
        try {
            Files.asCharSink(to, Charset.forName("UTF-8")).write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
